package com.hfzy.ihk.web.linkcall.handlers;

import com.alibaba.fastjson.JSON;
import com.hfzy.ihk.common.core.elasticsearch.ElasticsearchOperation;
import com.hfzy.ihk.common.core.elasticsearch.base.CommonTableOperationHandler;
import com.hfzy.ihk.common.util.string.StringUtil;
import com.hfzy.ihk.facade.linkcall.entity.flow.config.CustomerAnswerWay;
import com.hfzy.ihk.facade.linkcall.entity.flow.config.SpeakingFlowStep;
import com.hfzy.ihk.facade.linkcall.enums.LinkCallOperations;
import com.hfzy.ihk.facade.linkcall.enums.LinkCallTables;
import com.hfzy.ihk.web.linkcall.biz.flow.config.CustomerAnswerWayBiz;
import com.hfzy.ihk.web.linkcall.biz.flow.config.SpeakingFlowStepBiz;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
public class SpeakingFlowStepDelOperationHandler extends CommonTableOperationHandler {
    private static final Logger logger = LoggerFactory.getLogger(SpeakingFlowStepDelOperationHandler.class);

    @Autowired
    SpeakingFlowStepBiz speakingFlowStepBiz;
    @Autowired
    CustomerAnswerWayBiz customerAnswerWayBiz;

    public SpeakingFlowStepDelOperationHandler() {
        /**
         * 必须定义该处理器对应的table和操作名
         * */
        this.table = LinkCallTables.SPEAKING_FLOW_STEP;

        this.tableOperation = LinkCallOperations.delSpeakingFlowSteps;

    }

    @Override
    protected void toHandle(ElasticsearchOperation elasticsearchOperation) {
        if(elasticsearchOperation!=null){
            Object data = elasticsearchOperation.getData();
            logger.debug("handle data:{}", JSON.toJSONString(data));
            if(data != null || StringUtil.isNotNull(elasticsearchOperation.getOptMethod()))
                switch (elasticsearchOperation.getOptMethod()){
                    case "delSpeakingFlowSteps":
                        List<SpeakingFlowStep> speakingFlowStepList = (List<SpeakingFlowStep>) data ;
                        if(speakingFlowStepList!=null && speakingFlowStepList.size()>0){
                            List<CustomerAnswerWay> customerAnswerWays = new ArrayList<CustomerAnswerWay>();
                            List<String> flowStepIds = new ArrayList<String>();
                            for(SpeakingFlowStep speakingFlowStep:speakingFlowStepList){
                                flowStepIds.add(speakingFlowStep.getId());
                                List<CustomerAnswerWay> customTypes = customerAnswerWayBiz.getCustomerAnswerWayByFlowStepId(speakingFlowStep.getId());
                                if(customTypes!=null && !customTypes.isEmpty()){
                                    customerAnswerWays.addAll(customTypes);
                                }
                            }
                            //跟节点的 === 自定义回答也一并删除
                            if(customerAnswerWays!=null && !customerAnswerWays.isEmpty()){
                                List<String> customerAnswerWayIds = new ArrayList<String>();
                                for(CustomerAnswerWay customerAnswerWay:customerAnswerWays){
                                    customerAnswerWayIds.add(customerAnswerWay.getId());
                                }
                                customerAnswerWayBiz.deleteBatchIds(customerAnswerWayIds);
                            }
                            speakingFlowStepBiz.deleteBatchIds(flowStepIds);
                        }
                        break;
                    default:
                        break;
                }
        }
    }


}
